<?php
require_once 'admin_inc_path.php';

//data submission for left column
if (isset($_POST['slc'])):
    $lc = DAOFactory::getPA2DAO()->queryBySeg($_POST['seg']);
    if (empty($lc)):
        $sh = new SubHeading();
        $des = new Description();
        else:
            $sh = DAOFactory::getSubHeadingDAO()->load($lc[0]->sH);
            $des = DAOFactory::getDescriptionDAO()->load($lc[0]->dES);
    endif;

    $sh->textEn = $_POST['sub_heading_en'];
    $sh->textSv = $_POST['sub_heading_sv'];
    $des->textEn = $_POST['description_en'];
    $des->textSv = $_POST['description_sv'];

    if (empty($lc)):
        $sh_id = DAOFactory::getSubHeadingDAO()->insert($sh);
        $des_id = DAOFactory::getDescriptionDAO()->insert($des);
        $pA2 = new PA2();
        $pA2->sH = $sh_id;
        $pA2->dES = $des_id;
        $pA2->seg = $_POST['seg'];
        $response = DAOFactory::getPA2DAO()->insert($pA2);
        //New left segment has been inserted.
        echo __INSERTED__;
        //var_dump($response);
    else:
        $sh_id = DAOFactory::getSubHeadingDAO()->update($sh);
        $des_id = DAOFactory::getDescriptionDAO()->update($des);
         //Left segment have been updated.
        echo __UPDATED__;
        //var_dump($sh_id, $des_id);
    endif;

endif;

//new data insertion for page contents

if(isset ($_POST['snm'])) :
   
        $sh = new SubHeading();
        $des = new Description();
        $img = new Image();
        
        $sh->textEn = $_POST['sh_en'];
        $sh->textSv = $_POST['sh_sv'];
        
        $des->textEn = $_POST['description_en'];
        $des->textSv = $_POST['description_sv'];
        
        $errors = array();
        $imgCont = new FileController();
        if ($imgCont->attach_file($_FILES['image']) && $imgCont->process_image(_IMAGE_, 'index_page')) {
            $img->name = $imgCont->optimized_name;
            $img->path = 'temp';
           
        } else {
            
            echo __PIMGUP__;
            //print_r($imgCont->errors);
            
        }
        
        $pA2 = new PA2();
        $pA2->seg = $_POST['seg'];
        $pA2->sH = DAOFactory::getSubHeadingDAO()->insert($sh);
        $pA2->dES = DAOFactory::getDescriptionDAO()->insert($des);
        $pA2->iMAGE = DAOFactory::getImageDAO()->insert($img);
        
        $response = DAOFactory::getPA2DAO()->insert($pA2);
        if($response):
            //Information have been inserted.
            echo __INSERTED__;
        endif;
        //print_r($response);

endif;


//updation for page contents
if (isset ($_POST['update'])):
    
    $pA2 = DAOFactory::getPA2DAO()->load($_POST['ref_pc']);
    //print_r($pA2); die;

    $sh = DAOFactory::getSubHeadingDAO()->load($pA2->sH);
    $des = DAOFactory::getDescriptionDAO()->load($pA2->dES);
    $img = DAOFactory::getImageDAO()->load($pA2->iMAGE);
    
    $sh->textEn = $_POST['sh_en'];
    $sh->textSv = $_POST['sh_sv'];
        
    $des->textEn = $_POST['description_en'];
    $des->textSv = $_POST['description_sv'];
    
    if(isset ($_FILES['image'])):
    $errors = array();
        $imgCont = new FileController();
        if ($imgCont->attach_file($_FILES['image']) && $imgCont->process_image(_IMAGE_, 'index_page')) {
            $img->name = $imgCont->optimized_name;
            $img->path = 'temp';
           
        } else {
            echo __PIMGUP__;
            //print_r($imgCont->errors);
    }
    endif;
    
   
    $res_sh = DAOFactory::getSubHeadingDAO()->update($sh);
    $res_des = DAOFactory::getDescriptionDAO()->update($des);
    $res_img = DAOFactory::getImageDAO()->update($img);
    
    //Information has been updated
    echo __UPDATED__;
    //var_dump($res_sh,$res_des,$res_img);
    
endif;

//for deletion
if (isset ($_POST['del'])):
    $response = DAOFactory::getPA2DAO()->delete($_POST['ref_pc']);
    if($response):
        //Information have been deleted
        echo __REMOVED__;
    else:
        echo __PREMOVED__;
    endif;
    
endif;




include_once('inc/admin_header.php');
?>		



<div class="admin_content_wrapper">
    <h1>Information</h1>
    <h2>Left Column</h2>
    <?php
    //fetching data for left column
    $mm = DAOFactory::getMainMenuDAO()->load(1);
    $head = DAOFactory::getMainHeadingDAO()->load($mm->mH);
    $lc_list = DAOFactory::getPA2DAO()->queryBySeg(0);
    $sh = !empty($lc_list) ? DAOFactory::getSubHeadingDAO()->load($lc_list[0]->sH): '';
    $des = !empty($lc_list) ? DAOFactory::getDescriptionDAO()->load($lc_list[0]->dES): '';
    
    //fetching data for page contents
    
    ?>

    <form action="" method="POST">
        <div class="admin_segment">
            <table>
                <tr>
                    <td>

                    </td>
                    <td>
                        <h3>Svenska</h3>
                    </td>
                    <td>
                        <h3>Engelska</h3>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Heading:</h3>
                    </td>
                    <td>
                        <p><?php echo $value = (!empty($head->textSv)) ? $head->textSv : __EMT; ?> (Editable by Top Meny)</p>
                    </td>
                    <td>
                        <p><?php echo $value = (!empty($head->textEn)) ? $head->textEn : __EMT; ?> (Editable by Top Meny)</p>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Sub Heading:</h3>
                    </td>
                    <td>
                        <input type="text" name="sub_heading_sv" value="<?php echo $value=(!empty($sh->textSv)) ? $sh->textSv : __EMT; ?>" />
                    </td>
                    <td>
                        <input type="text" name="sub_heading_en" value="<?php echo $value=(!empty($sh->textEn)) ? $sh->textEn : __EMT; ?>" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Description:</h3>
                    </td>
                    <td>
                        <textarea name="description_sv"><?php echo $value=(!empty($des->textSv)) ? $des->textSv : __EMT; ?> </textarea>
                    </td>
                    <td>
                        <textarea name="description_en"><?php echo $value=(!empty($des->textEn)) ? $des->textEn : __EMT; ?></textarea>
                    </td>
                </tr>
            </table>
            <input type="hidden" name="seg" value="0" />
            <input type="submit" value="Save and Upload" name="slc"/>
        </div>
    </form>

   
    
    
    
    <h2>Page Content</h2>
    
    
    <?php 
     $pc = DAOFactory::getPA2DAO()->queryBySeg(1);
     if(!empty($pc)):
         //print_r($pc);
         foreach ($pc as $key => $value):
            echo $value->id;
            $img = DAOFactory::getImageDAO()->load($value->iMAGE);
            $sh = DAOFactory::getSubHeadingDAO()->load($value->sH);
            $des = DAOFactory::getDescriptionDAO()->load($value->dES);
    ?>
    
    <form action="" method="POST" enctype="multipart/form-data">
        <div class="admin_segment">
            <table>
                <tr>
                    <td>

                    </td>
                    <td>
                        <h3>Svenska</h3>
                    </td>
                    <td>
                        <h3>Engelska</h3>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Image:</h3>
                    </td>
                    <td>
                        <input type="file" name="image"/>
                    </td>
                    <td>
                        <p> <?php echo $img->name; ?>  </p>    
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Heading:</h3>
                    </td>
                    <td>
                        <input type="text" name="sh_sv" value="<?php echo $sh->textSv; ?>"/>
                    </td>
                    <td>
                        <input type="text" name="sh_en" value="<?php echo $sh->textEn; ?>"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Description:</h3>
                    </td>
                    <td>
                        <textarea name="description_sv"><?php echo $des->textSv; ?></textarea>
                    </td>
                    <td>
                        <textarea name="description_en"><?php echo $des->textEn; ?></textarea>
                    </td>
                </tr>
            </table>
            <input type="hidden" value="1" name="seg"/>
            <input type="submit" value="Update" name="update"/>
            <input type="hidden" value="<?php echo $value->id; ?>" name="ref_pc" />
            <input class="delete_btn" type="submit" value="Delete" name="del"/>
        </div>
    </form>
    
    <?php
     endforeach;
     endif;
    ?>
  
  
    <form action="" method="POST" enctype="multipart/form-data">
        <div class="admin_segment">
            <table>
                <tr>
                    <td>

                    </td>
                    <td>
                        <h3>Svenska</h3>
                    </td>
                    <td>
                        <h3>Engelska</h3>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Image:</h3>
                    </td>
                    <td>
                        <input type="file" name="image"/>
                    </td>
                    <td>

                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Heading:</h3>
                    </td>
                    <td>
                        <input type="text" name="sh_sv"/>
                    </td>
                    <td>
                        <input type="text" name="sh_en"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h3>Description:</h3>
                    </td>
                    <td>
                        <textarea name="description_sv"> </textarea>
                    </td>
                    <td>
                        <textarea name="description_en"> </textarea>
                    </td>
                </tr>
            </table>
            <input type="hidden" value="1" name="seg"/>
            <input type="submit" value="Save and Upload" name="snm"/>
        </div>
    </form>

   

  

</div>
<?php include_once('inc/admin_footer.php') ?>